package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.entity.po.Carousel;
import com.travel.mapper.CarouselMapper;
import com.travel.result.PageResult;
import com.travel.service.CarouselService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {

    private final CarouselMapper carouselMapper;

    public CarouselServiceImpl(CarouselMapper carouselMapper) {
        this.carouselMapper = carouselMapper;
    }

    @Override
    public List<Carousel> getAllCarousels() {
        log.info("获取所有轮播图");
        return carouselMapper.selectList(null);
    }

    @Override
    public List<Carousel> getActiveCarousels() {
        LambdaQueryWrapper<Carousel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Carousel::getIsActive, 1);
        return carouselMapper.selectList(wrapper);
    }

    @Override
    public void addCarousel(Carousel carousel) {
        carouselMapper.insert(carousel);
    }

    @Override
    public PageResult<Carousel> getCarouselsByPage(Integer pageNum, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<Carousel> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Carousel::getId);

        // 执行分页查询
        Page<Carousel> page = new Page<>(pageNum, pageSize);
        carouselMapper.selectPage(page, wrapper);

        // 构建分页结果
        return new PageResult<>(
                page.getRecords(),
                page.getTotal(),
                page.getPages(),
                page.getCurrent(),
                page.getSize()
        );
    }
} 